from PyQt5.QtWidgets import QWidget, QVBoxLayout, QSlider, QLabel, QPushButton, QComboBox, QGroupBox
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QColor, QPalette

class ControlPanel(QWidget):
    control_signal = pyqtSignal(dict)
    mode_changed = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.controls = {
            'throttle': 50,
            'yaw': 0,
            'pitch': 0,
            'roll': 0,
            'flight_mode': 'Manual'
        }
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()
        
        # 飞行模式选择
        mode_group = QGroupBox("Flight Mode")
        mode_layout = QVBoxLayout()
        self.mode_selector = QComboBox()
        self.mode_selector.addItems(['Manual', 'Altitude Hold', 'Position Hold', 'Auto'])
        self.mode_selector.currentTextChanged.connect(self.change_mode)
        mode_layout.addWidget(self.mode_selector)
        mode_group.setLayout(mode_layout)
        layout.addWidget(mode_group)
        
        # 油门控制
        throttle_group = QGroupBox("Throttle")
        throttle_layout = QVBoxLayout()
        self.throttle = QSlider(Qt.Vertical)
        self.throttle.setRange(0, 100)
        self.throttle.setValue(50)
        self.throttle.setStyleSheet(self.get_style('#4CAF50'))
        throttle_layout.addWidget(self.throttle)
        self.throttle_label = QLabel("50%")
        throttle_layout.addWidget(self.throttle_label)
        throttle_group.setLayout(throttle_layout)
        layout.addWidget(throttle_group)
        
        # 偏航控制
        yaw_group = QGroupBox("Yaw")
        yaw_layout = QVBoxLayout()
        self.yaw = QSlider(Qt.Horizontal)
        self.yaw.setRange(-100, 100)
        self.yaw.setValue(0)
        self.yaw.setStyleSheet(self.get_style('#2196F3'))
        yaw_layout.addWidget(self.yaw)
        self.yaw_label = QLabel("0°")
        yaw_layout.addWidget(self.yaw_label)
        yaw_group.setLayout(yaw_layout)
        layout.addWidget(yaw_group)
        
        # 俯仰控制
        pitch_group = QGroupBox("Pitch")
        pitch_layout = QVBoxLayout()
        self.pitch = QSlider(Qt.Horizontal)
        self.pitch.setRange(-100, 100)
        self.pitch.setValue(0)
        self.pitch.setStyleSheet(self.get_style('#FF9800'))
        pitch_layout.addWidget(self.pitch)
        self.pitch_label = QLabel("0°")
        pitch_layout.addWidget(self.pitch_label)
        pitch_group.setLayout(pitch_layout)
        layout.addWidget(pitch_group)
        
        # 横滚控制
        roll_group = QGroupBox("Roll")
        roll_layout = QVBoxLayout()
        self.roll = QSlider(Qt.Horizontal)
        self.roll.setRange(-100, 100)
        self.roll.setValue(0)
        self.roll.setStyleSheet(self.get_style('#9C27B0'))
        roll_layout.addWidget(self.roll)
        self.roll_label = QLabel("0°")
        roll_layout.addWidget(self.roll_label)
        roll_group.setLayout(roll_layout)
        layout.addWidget(roll_group)
        
        # 应急按钮
        self.emergency_btn = QPushButton("Emergency Stop")
        self.emergency_btn.setStyleSheet("background-color: #f44336; color: white")
        layout.addWidget(self.emergency_btn)
        
        # 连接信号
        self.throttle.valueChanged.connect(self.update_throttle)
        self.yaw.valueChanged.connect(self.update_yaw)
        self.pitch.valueChanged.connect(self.update_pitch)
        self.roll.valueChanged.connect(self.update_roll)
        self.emergency_btn.clicked.connect(self.emergency_stop)
        
        self.setLayout(layout)

    def get_style(self, color):
        return f"""
            QSlider::groove:horizontal {{ height: 8px; background: #eee; }}
            QSlider::handle:horizontal {{ width: 20px; background: {color}; }}
            QSlider::add-page:horizontal {{ background: {color}; }}
        """

    def update_throttle(self, value):
        self.controls['throttle'] = value
        self.throttle_label.setText(f"{value}%")
        self.emit_control()

    def update_yaw(self, value):
        self.controls['yaw'] = value
        self.yaw_label.setText(f"{value}°")
        self.emit_control()

    def update_pitch(self, value):
        self.controls['pitch'] = value
        self.pitch_label.setText(f"{value}°")
        self.emit_control()

    def update_roll(self, value):
        self.controls['roll'] = value
        self.roll_label.setText(f"{value}°")
        self.emit_control()

    def change_mode(self, mode):
        self.controls['flight_mode'] = mode
        self.mode_changed.emit(mode)
        self.emit_control()

    def emergency_stop(self):
        self.controls = {
            'throttle': 0,
            'yaw': 0,
            'pitch': 0,
            'roll': 0,
            'flight_mode': 'Emergency'
        }
        self.throttle.setValue(0)
        self.yaw.setValue(0)
        self.pitch.setValue(0)
        self.roll.setValue(0)
        self.emit_control()

    def emit_control(self):
        self.control_signal.emit(self.controls.copy())